package com.sunny;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author zsunny
 * @Date 2018/9/12 9:55
 * @Mail zsunny@yeah.net
 */
public class Demo {

    private CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
    private Object a = new Object();
    private Object b = new Object();

    class ThreadA extends Thread{
        @Override
        public void run() {
            synchronized (a){
                try {
                    System.out.println("A开始阻塞");
                    cyclicBarrier.await();
                    System.out.println("A阻塞完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                synchronized (b){

                }
            }
        }
    }

    class ThreadB extends Thread{
        @Override
        public void run() {
            synchronized (b) {
                try {
                    System.out.println("B开始阻塞");
                    cyclicBarrier.await();
                    System.out.println("B阻塞完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                synchronized (a) {

                }
            }
        }
    }

    public static void main(String[] args) {

        Demo demo = new Demo();

        demo.new ThreadA().start();
        demo.new ThreadB().start();

    }

}
